package cn.picclife.mwx.manager.sys.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.picclife.mwx.manager.sys.mapper.SysTipMessageMapper;
import cn.picclife.mwx.manager.sys.model.bo.DataResponse;
import cn.picclife.mwx.manager.sys.model.bo.TipMessageListQueryReq;
import cn.picclife.mwx.manager.sys.model.bo.TipQueryReq;
import cn.picclife.mwx.manager.sys.model.bo.TipSaveReq;
import cn.picclife.mwx.manager.sys.model.bo.TipUpdateReq;
import cn.picclife.mwx.manager.sys.model.po.DateList;
import cn.picclife.mwx.manager.sys.model.po.SysTipMessageEntity;
import cn.picclife.mwx.manager.sys.service.TipMessageService;
import cn.picclife.mwx.manager.util.enums.ResponseStatusEnum;

/**
 * @description 提示信息service实现类 
 * @author SaiZhiyi
 * @creatTime 2020年7月16日 上午11:03:37
 */
@Service
public class TipMessageServiceImpl implements TipMessageService {
	private static final Log log = LogFactory.getLog(TipMessageServiceImpl.class);
	private static final String queryListCode = "000001"; 
	private static final String tipSaveCode = "000002"; 
	private static final String tipQueryCode = "000003"; 
	private static final String tipUpdateCode = "000004"; 
	
	@Autowired
	private SysTipMessageMapper sysTipMessageMapper;

	/**
	 * @description 查询提示信息列表 
	 * @author saizhiyi
	 * @param req
	 * @return
	 */
	@Override
	public DataResponse queryTipList(TipMessageListQueryReq req) {
		DataResponse res = new DataResponse();
		log.info("[查询提示信息列表]" + queryListCode + "======[开始]");
		DateList date = new DateList();
		int count = sysTipMessageMapper.queryCount(req);
		if(count%req.getPageSize() == 0){
			date.setAllPage(count/req.getPageSize());
		}else{
			date.setAllPage(count/req.getPageSize() + 1);
		}
		date.setNowPage(req.getPageNo());
		List<SysTipMessageEntity> list = sysTipMessageMapper.queryList(req);
		if(list != null){
			date.setDate(list);
		}
		List<DateList> dlist = new ArrayList<DateList>();
		dlist.add(date);
		res.setData(dlist);
		res.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
		res.setMsg("查询成功");
		log.info("[查询提示信息列表]" + queryListCode + "======[结束]");
		return res;
	}

	/**
	 * @description 提示信息新增保存
	 * @author saizhiyi
	 * @param req
	 * @return
	 */
	@Override
	public DataResponse tipSave(TipSaveReq req) {
		DataResponse res = new DataResponse();
		log.info("[新增提示信息]" + tipSaveCode + "======[开始]");
		int num = sysTipMessageMapper.tipSave(req);
		if(num == 1){
			res.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
			res.setMsg("新增成功");
		}else{
			res.setCode(ResponseStatusEnum.FAIL_CODE.getCode());
			res.setMsg("您的新增信息中(产品编码、提示编码、提示信息版本号)与其他提示信息重复");
		}
		log.info("[新增提示信息]" + tipSaveCode + "======[结束]");
		return res;
	}

	/**
	 * @description 提示信息详情查询 
	 * @author saizhiyi
	 * @param req
	 * @return
	 */
	@Override
	public DataResponse tipQuery(TipQueryReq req) {
		DataResponse res = new DataResponse();
		log.info("[查询提示信息详情]" + tipQueryCode + "======[开始]");
		List<SysTipMessageEntity> list = sysTipMessageMapper.tipQuery(req);
		if(list != null){
			res.setData(list);
		}
		res.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
		res.setMsg("查询成功");
		log.info("[查询提示信息详情]" + tipQueryCode + "======[结束]");
		return res;
	}

	/**
	 * @description 提示信息修改 
	 * @author saizhiyi
	 * @param req
	 * @return
	 */
	@Override
	public DataResponse tipUpdate(TipUpdateReq req) {
		DataResponse res = new DataResponse();
		log.info("[修改提示信息]" + tipUpdateCode + "======[开始]");
		TipMessageListQueryReq listReq = new TipMessageListQueryReq();
		listReq.setProductCode(req.getProductCode());
		listReq.setTipCode(req.getTipCode());
		listReq.setTipVersion(req.getTipVersion());
		listReq.setProductName("");
		List<SysTipMessageEntity> queryList = sysTipMessageMapper.queryList(listReq);
		log.info("查询成功");
		if(queryList != null && queryList.size() != 0){
			res.setCode(ResponseStatusEnum.UPDATE_FAIL_CODE.getCode());
			if(req.getTipId() != queryList.get(0).getTipid()){
				res.setMsg("您的修改信息中(产品编码、提示编码、提示信息版本号)与其他提示信息重复");
			}else{
				res.setMsg("修改提示信息时，提示信息版本号为必要修改项");
			}
			return res;
		} 
		sysTipMessageMapper.tipUpdate(req);			
	 	res.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
	 	res.setMsg("修改成功");
		log.info("[修改提示信息]" + tipUpdateCode + "======[结束]");
		return res;
	}

}          
